home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 116 / MacAddict 116 (Mac Power Pack)(theDISC)(April 2006).iso / Software / Interface / HolidayWidgetBundle.dmg / / NotePad.wdgt / NotePad.js < prev    next >
Encoding:
Text File  |  2005-11-17  |  26.9 KB  |  1,233 lines

  1. Ôªøvar openPage = 1, flipped;
  2.  
  3. /* Event handlers */
  4. function load () {    
  5.     checkForHome();
  6.     loadLanguage();
  7.     
  8.     //load font size
  9.     document.getElementById("fontsize").value = widget.preferenceForKey("font-size");
  10.     
  11.     if (widget.preferenceForKey("font-size") == "")
  12.         document.getElementById("fontsize").value = "11 pt";
  13.     
  14.     
  15.     var prefWidth = widget.preferenceForKey("pad-width");
  16.     var prefHeight = widget.preferenceForKey("pad-height");
  17.     
  18.     if (prefWidth && prefWidth > 0 && prefHeight && prefHeight > 0) {
  19.         window.width = prefWidth;
  20.         window.height = prefHeight;
  21.         window.resizeTo(prefWidth, prefHeight);
  22.     }
  23.     
  24.     var prefPage = widget.preferenceForKey("open-page");
  25.     
  26.     var pageToShow = 1;
  27.     if (prefPage && prefPage > 0 ) {
  28.         pageToShow = prefPage;
  29.     }
  30.     
  31.     setFontSize();
  32.     
  33.     // Load the first page
  34.     openPage = pageToShow;
  35.     loadPage(pageToShow);
  36.     loadPageSelector();
  37. }
  38.  
  39. function store() {
  40.     savePage(openPage);
  41. }
  42.  
  43. /* Neat fade effects */
  44. var showControls;
  45.  
  46. function doBlur() {
  47.     store();
  48.     showControls = false;
  49.     doFade();
  50. }
  51.  
  52. function doFocus() {
  53.     showControls = true;
  54.     doFade();
  55. }
  56.  
  57. /*
  58.     since the dashboard sucks at giving focus, we need 
  59.     a few extra checks
  60. */
  61. function extraFocus() {
  62.     if (!showControls)
  63.         doFocus();
  64. }
  65.  
  66. var fTools, fControls, fHider;
  67.  
  68. function doFade() {
  69.     clearInterval(fTools);
  70.     fTools        = qfade("tools");
  71.     
  72.     clearInterval(fControls);
  73.     fControls    = qfade("bottomcontrols");
  74.     
  75.     clearInterval(fHider);
  76.     fHider        = qfade("toolhider");
  77. }
  78.  
  79. function getTO() {
  80.      if (showControls)
  81.         return 1;
  82.     else
  83.         return 0;
  84. }
  85.  
  86. window.onblur = doBlur;
  87. window.onfocus = doFocus;
  88. widget.onhide = store;
  89. widget.onremove = store;
  90.  
  91. var help_canvas;
  92.  
  93. // Checks for and creates the Library folder where note pad pages are stored
  94. function checkForHome() {
  95.     if (widget.system("ls ~/Library/NotePad",null).status==1) {
  96.         widget.system("mkdir ~/Library/NotePad",null);
  97.     }
  98. }
  99.  
  100.  
  101.  
  102.  
  103. /* Languages */
  104.  
  105. var language = "english";
  106. function loadLanguage() {
  107.     var l = widget.system('defaults read "Apple Global Domain" AppleLanguages',null).outputString;
  108.     language = getLanguageToUse(splitLanguages(l));
  109.     
  110.     document.getElementById("deltext").innerHTML = getLocalString("delete sure");
  111.     document.getElementById("printtext").innerHTML = getLocalString("print sure");
  112.     
  113.     document.getElementById("delok").innerHTML = getLocalString("delete");
  114.     document.getElementById("printok").innerHTML = getLocalString("print");
  115.     
  116.     document.getElementById("delcancel").innerHTML = getLocalString("cancel");
  117.     document.getElementById("printcancel").innerHTML = getLocalString("cancel");
  118.         
  119.     document.getElementById("deleteicon").title = getLocalString("delete");
  120.     document.getElementById("printicon").title = getLocalString("print");
  121.     document.getElementById("newicon").title = getLocalString("new page");
  122.     document.getElementById("searchicon").title = getLocalString("search notes");
  123. }
  124.  
  125. function splitLanguages(list) {
  126.     var list = list.substr(1);
  127.     list = list.substr(0,list.length-1);
  128.     var langs = list.split(",");
  129.     
  130.     return langs;
  131. }
  132.  
  133. function getLanguageToUse(langs) {
  134.     for (var i = 0; i < langs.length; i++) {
  135.         var l = langs[i];
  136.         
  137.         if (l.charAt(0) == ' ')
  138.             l = l.substr(1);
  139.         
  140.         switch (l) {
  141.             case "en":
  142.                 return "english";
  143.             case "English":
  144.                 return "english";
  145.                 
  146.             case "sv":
  147.                 return "swedish";
  148.             case "Swedish":
  149.                 return "swedish";
  150.                 
  151.             case "fr":
  152.                 return "french";
  153.             case "French":
  154.                 return "french";
  155.         }
  156.     }
  157.     
  158.     return "english";
  159. }
  160.  
  161. function getLocalString(value) {
  162.     
  163.     switch (value) {
  164.     
  165.         case "new page":
  166.             if (language == "english")
  167.                 return "New page";
  168.             else if (language == "french")
  169.                 return "Nouvelle page";
  170.             else if (language == "swedish")
  171.                 return "Ny sida";
  172.         
  173.         case "delete sure":
  174.             if (language == "english")
  175.                 return "Are you sure you want to delete this page?";
  176.             else if (language == "french")
  177.                 return "√ätes-vous s√ªr de vouloir supprimer cette page?";
  178.             else if (language == "swedish")
  179.                 return "√Ñr du s√§ker p√• att du vill radera sidan?";
  180.                 
  181.         case "print sure":
  182.             if (language == "english")
  183.                 return "Are you sure you want to print this page?";
  184.             else if (language == "french")
  185.                 return "√ätes-vous s√ªr de vouloir imprimer cette page?";
  186.             else if (language == "swedish")
  187.                 return "√Ñr du s√§ker p√• att du vill skriva ut sidan?";
  188.                 
  189.         case "delete":
  190.             if (language == "english")
  191.                 return "Delete";
  192.             else if (language == "french")
  193.                 return "Effacer";
  194.             else if (language == "swedish")
  195.                 return "Radera";
  196.                 
  197.         case "print":
  198.             if (language == "english")
  199.                 return "Print";
  200.             else if (language == "french")
  201.                 return "Imprimer";
  202.             else if (language == "swedish")
  203.                 return "Skriv ut";
  204.                 
  205.         case "cancel":
  206.             if (language == "english")
  207.                 return "Cancel";
  208.             else if (language == "french")
  209.                 return "Annuler";
  210.             else if (language == "swedish")
  211.                 return "Avbryt";
  212.         
  213.         case "search notes":
  214.             if (language == "english")
  215.                 return "Search notes";
  216.             else if (language == "french")
  217.                 return "Chercher";
  218.             else if (language == "swedish")
  219.                 return "S√∂kning";
  220.  
  221.         case "daysleft_pre":
  222.             if (language == "english")
  223.                 return "";
  224.             else if (language == "french")
  225.                 return "Vous allez pouvoir utiliser ";
  226.             else if (language == "swedish")
  227.                 return "";
  228.         
  229.         case "daysleft_mid":
  230.             if (language == "english")
  231.                 return " will run without payment for ";
  232.             else if (language == "french")
  233.                 return " de mani√®re illimit√©e pour encore ";
  234.             else if (language == "swedish")
  235.                 return " kommer att forts√§tta fungera gratis i ";
  236.         
  237.         case "daysleft_post":
  238.             if (language == "english")
  239.                 return " more day";
  240.             else if (language == "french")
  241.                 return " jour";
  242.             else if (language == "swedish")
  243.                 return " dag";
  244.         
  245.         case "daysleft_plural":
  246.             if (language == "english")
  247.                 return "s";
  248.             else if (language == "french")
  249.                 return "s";
  250.             else if (language == "swedish")
  251.                 return "ar";
  252.         
  253.         case "daysleft_thanks":
  254.             if (language == "english")
  255.                 return "Thanks for using NotePad!";
  256.             else if (language == "french")
  257.                 return "Merci d'utiliser NotePad!";
  258.             else if (language == "swedish")
  259.                 return "Tack f√∂r att du anv√§nder NotePad!";
  260.         
  261.         case "buy":
  262.             if (language == "english")
  263.                 return "Buy";
  264.             else if (language == "french")
  265.                 return "Acheter";
  266.             else if (language == "swedish")
  267.                 return "K√∂p";
  268.         
  269.         case "register":
  270.             if (language == "english")
  271.                 return "Register";
  272.             else if (language == "french")
  273.                 return "Enregistrer";
  274.             else if (language == "swedish")
  275.                 return "Registrera";
  276.     }
  277.     
  278. }
  279.  
  280.  
  281.  
  282. /* Page managing */
  283.  
  284. // Save this page, and load another.
  285. function goPage(num) {
  286.     savePage(openPage);
  287.     openPage = num;
  288.     loadPage(openPage);
  289. }
  290.  
  291. // Load a page from disk and display it, or create a new page if it didn't exist.
  292. function loadPage(page) {
  293.     var eDiv = document.getElementById("edit");
  294.     var data = widget.system("/bin/cat "+getFileName(page),null).outputString;
  295.     var title = getTitle(page);
  296.     
  297.     if (data != undefined) {
  298.         document.getElementById("title").innerHTML = title;
  299.         eDiv.value = data;
  300.         showCurrentPage();
  301.     } else {
  302.         document.getElementById("title").innerHTML = "New page "+openPage;
  303.         eDiv.value = getLocalString("new page")+' '+openPage;
  304.         savePage(openPage);
  305.         loadPageSelector();
  306.     }
  307.     
  308.     widget.setPreferenceForKey(openPage, "open-page");
  309.     // we don't want to keep the user waiting, so let's thread these as well
  310.     setTimeout("showTitle()",5);
  311.     setTimeout("updateNav()",6);
  312. }
  313.  
  314. function updateNav() {
  315.     if (openPage == 1) 
  316.         document.getElementById("prev").style.opacity = "0.2";
  317.     else
  318.         document.getElementById("prev").style.opacity = "0.5";
  319.     
  320.     if ( pageExists(openPage+1) )
  321.         document.getElementById("next").style.opacity = "0.5";
  322.     else
  323.         document.getElementById("next").style.opacity = "0.2";
  324. }
  325.  
  326. // Save a page to disk.
  327. function savePage(page) {
  328.     var txt = document.getElementById("edit").value;
  329.     
  330.     if (openPage>0 && txt!='' && !(txt.substr(0,9)=='New page ' && txt.length<12)) {
  331.         var writing = widget.system("/bin/cat > "+getFileName(page),forceAsync);
  332.         writing.write(txt);
  333.         writing.close();
  334.         updatePageSelector(page);
  335.         showTitle();
  336.     }
  337. }
  338.  
  339. // Delete a page from disk, and scootch other page numbers up
  340. function deletePage(page) {
  341.     widget.system("/bin/rm "+getFileName(page),null);
  342.     
  343.     var max = document.getElementById("pagePopup").options.length;
  344.     for (var p = page; p<max; p++) { //    for (var p = page; pageExists(p+1); p++) {
  345.         widget.system("/bin/mv "+getFileName(p+1)+" "+getFileName(p),null);
  346.     }
  347.     
  348.     loadPageSelector();
  349.     if ( (!pageExists(openPage)) && openPage>1) {
  350.         openPage--;
  351.     }
  352.     loadPage(openPage);
  353. }
  354.  
  355. function getTitle(page) {
  356.     var data = widget.system("/usr/bin/head -n1 "+getFileName(page),null).outputString;
  357.     
  358.     if (data != undefined) {
  359.         var title = data.substr(0,31);
  360.         
  361.         if (title != data)
  362.             title = title + "...";
  363.         
  364.         return title;
  365.     } else {
  366.         return "empty";
  367.     }
  368. }
  369.  
  370. function showTitle() {
  371.     if (document.getElementById("searchtab").style.display=="block")
  372.         document.getElementById("title").innerHTML = getLocalString("search notes");
  373.     else
  374.         document.getElementById("title").innerHTML = quickTitle(); //getTitle(openPage);
  375. }
  376.  
  377. /* Live title updating */
  378. var lastType = 0;
  379. function isTyping() {
  380.     var d = new Date();
  381.     var m = d.getMilliseconds();
  382.     
  383.     lastType = m;
  384.     setTimeout("isStillTyping("+m+")",500);
  385. }
  386.  
  387. function isStillTyping(m) {
  388.     if (m == lastType)
  389.         doUpdateTitle();
  390. }
  391.  
  392. function doUpdateTitle() {
  393.     document.getElementById("title").innerHTML = quickTitle();
  394.     isUpdating = false;
  395. }
  396.  
  397. function quickTitle() {
  398.     var txt = document.getElementById("edit").value;
  399.     var fl = firstLine(txt);
  400.     
  401.     if (fl != "")
  402.         return fl;
  403.     
  404.     return txt.substr(0,31);
  405. }
  406.  
  407. function firstLine(text) {
  408.     var delim = text.indexOf("\n");
  409.     return text.substr(text,delim+1);    
  410. }
  411.  
  412.  
  413.  
  414.  
  415.  
  416. /* Misc. file managing */
  417.  
  418. // Where to save pages?
  419. function getFileName(page) {
  420.     return "~/Library/NotePad/Page"+page+".txt";
  421. }
  422.  
  423. // Does a page exist on disk?
  424. function pageExists(p) {
  425.     return (widget.system("/bin/ls "+getFileName(p),null).outputString!=undefined);
  426. }
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433. /* Search */
  434.  
  435. function togglesearch(opening) {
  436.     store();
  437.     toggleVisibility("edittab");
  438.     toggleVisibility("searchtab");
  439.     toggleVisibility("controls");
  440.     toggleVisibility("searchicon");
  441.     toggleVisibility("printicon");
  442.     toggleVisibility("deleteicon");
  443.     toggleVisibility("newicon");
  444.     showTitle();
  445.     
  446.     if (opening == true)
  447.         document.getElementById("searchtext").focus();
  448. }
  449.  
  450. function showresult(page) {
  451.     goPage(page);
  452.     togglesearch();
  453. }
  454.  
  455. var grepProc;
  456. function doSearch() {
  457.     if (grepProc != undefined) {
  458.         grepCompleted();
  459.         grepProc.cancel();
  460.     }
  461.     
  462.     var searchstring = document.getElementById("searchtext").value;
  463.     
  464.     if (searchstring == "") {
  465.         parseGrep("");
  466.         return;
  467.     }
  468.     
  469.     /*
  470.         Multi-byte search support (asian text keke ^_^)
  471.     */
  472.     
  473.     var writing = widget.system("/bin/cat > ~/Library/Caches/NotePadSearch",forceAsync);
  474.     writing.write(searchstring);
  475.     writing.close();
  476.     
  477.     grepProc = widget.system("grep -i -m 1 -f ~/Library/Caches/NotePadSearch ~/Library/NotePad/*",grepCompleted);
  478.  
  479.     /*
  480.         This line is for single-byte languages. It's faster.
  481.         grepProc = widget.system("grep -i -m 1 \""+searchstring+"\" ~/Library/NotePad/*",grepCompleted);
  482.     */
  483. }
  484.  
  485. function grepCompleted() {
  486.     parseGrep(grepProc.outputString);
  487. }
  488.  
  489. function parseGrep(grepOutput) {
  490.     var results = document.getElementById("searchresults");
  491.     var s = "<br>";
  492.     
  493.     if (grepOutput != undefined) {
  494.         var resultarray = grepOutput.split("\n");
  495.         
  496.         for (var i = 0; i < resultarray.length; i++) {
  497.             var line = resultarray[i];
  498.             var delim = line.indexOf(":");
  499.             var path = line.substr(0,delim);
  500.             var foundLine = line.substr(delim+1);
  501.             
  502.             j = path.lastIndexOf("e"); // e as in Page
  503.             
  504.             if (j > 0) {
  505.                 p = parseInt(path.substr(j+1,3));
  506.                 
  507.                 if (p != NaN) {
  508.                     link = "" + p + ". " + foundLine; //getTitle(p);
  509.                     link = link.link("javascript:showresult("+p+")");
  510.                     s = s + link + "<br> \n";
  511.                 }
  512.             }
  513.         }
  514.     }
  515.     
  516.     results.innerHTML = s;
  517. }
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524. /* Manage the page selector */
  525. function loadPageSelector() {
  526.     var eDiv = document.getElementById("pagePopup");
  527.     eDiv.options.length = 0;
  528.     
  529.     for (var p = 1; pageExists(p); p++) {
  530.         eDiv.options[p-1] = new Option(p+". " + getTitle(p),p);
  531.     }
  532.     showCurrentPage();
  533. }
  534.  
  535. function updatePageSelector(p) {
  536.     var eDiv = document.getElementById("pagePopup");
  537.     eDiv.options[p-1] = new Option(p+". " + getTitle(p),p);
  538.     showCurrentPage();
  539. }
  540.  
  541. function showCurrentPage() {
  542.     document.getElementById("pagePopup").value = openPage;
  543.     document.getElementById("pagenumber").innerHTML = openPage;
  544. }
  545.  
  546.  
  547.  
  548.  
  549.  
  550. /* Page flippers */
  551.  
  552. // Next page flipper
  553. function nextPage() {
  554.     if ( pageExists(openPage+1) )
  555.         goPage(parseInt(openPage)+1);
  556. }
  557.  
  558. // Previous page flipper
  559. function prevPage() {
  560.     if (openPage > 1) {
  561.         goPage(parseInt(openPage)-1);
  562.     }
  563. }
  564.  
  565. function newPage() {
  566.     goPage( document.getElementById("pagePopup").options.length +1 );
  567. }
  568.  
  569. function newPageDrop(event)  {
  570.     var text = null;
  571.     
  572.     try {
  573.         text = event.dataTransfer.getData("text/plain");    
  574.     } catch (ex)    {    }
  575.     
  576.     if (text) {
  577.         newPage();
  578.         document.getElementById("edit").value = text;
  579.         doUpdateTitle()
  580.     }
  581.  
  582.     event.stopPropagation();
  583.     event.preventDefault();
  584. }
  585.  
  586.  
  587. function dragenter (event)
  588. {
  589.     event.stopPropagation();
  590.     event.preventDefault();
  591. }
  592.  
  593. function dragover (event)
  594. {
  595.     event.stopPropagation();
  596.     event.preventDefault();
  597. }
  598.  
  599. function dragleave (event)
  600. {
  601.     event.stopPropagation();
  602.     event.preventDefault();
  603. }
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613. /* Dialogs */
  614.  
  615. function deleteQuery() {
  616.     toggleVisibility("deletedialog");
  617.     toggleVisibility("controls");
  618.     toggleVisibility("searchicon");
  619.     toggleVisibility("printicon");
  620.     toggleVisibility("newicon");
  621. }
  622.  
  623. function deleteYes() {
  624.     deleteQuery();
  625.     deletePage(openPage);
  626. }
  627.  
  628. function printQuery() {
  629.     toggleVisibility("printdialog");
  630.     toggleVisibility("controls");
  631.     toggleVisibility("searchicon");
  632.     toggleVisibility("deleteicon");
  633.     toggleVisibility("newicon");
  634. }
  635.  
  636. function printYes() {
  637.     printQuery();
  638.     printPage(openPage);
  639. }
  640.  
  641.  
  642.  
  643.  
  644.  
  645. /* Utilities */
  646. function printPage(page) {
  647.     widget.system("/usr/bin/lpr " + getFileName(page), null);
  648.     //NotePlug.printText(document.getElementById("edit").value);
  649. }
  650.  
  651.  
  652.  
  653. function waitCursor(what) {
  654.     if (what)
  655.         document.body.style.cursor="wait";
  656.     else
  657.         document.body.style.cursor="auto";
  658. }
  659.  
  660. function toggleVisibility(what) {
  661.     if (document.getElementById(what).style.display=="none")
  662.         document.getElementById(what).style.display="block";
  663.     else
  664.         document.getElementById(what).style.display="none";
  665. }
  666.  
  667.  
  668. function forceRepaint() {
  669.     var v = document.body.offsetHeight;
  670. }
  671.  
  672. function forceAsync() { /* blank */ }
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679. /* Tutorial */
  680. var help_running = false;
  681.  
  682. function getOffsetTop(element) {
  683.     var top = 0;
  684.     var el = element;
  685.     
  686.     do {
  687.         top += el.offsetTop;
  688.         el = el.parentNode;
  689.     } while (el !== document.body);
  690.     
  691.     return top;
  692. }
  693.  
  694. function getOffsetLeft(element) {
  695.     var left = 0;
  696.     var el = element;
  697.     
  698.     do {
  699.         left += el.offsetLeft;
  700.         el = el.parentNode;
  701.     } while (el !== document.body);
  702.     
  703.     return left;
  704. }
  705.  
  706. var help_canvas_pen, help_canvas_x_stretch, help_canvas_y_stretch;
  707.  
  708. function showHelp() {
  709.     help_running = true;
  710.  
  711.     hidePrefs();
  712.     
  713.     /* If the search tab is visible, hide it */
  714.     if (document.getElementById('searchtab').style.display!="none")
  715.         togglesearch(false);
  716.         
  717.     /* Hide dialog boxes */
  718.     if (document.getElementById('printdialog').style.display!="none")
  719.         printQuery();
  720.  
  721.     if (document.getElementById('deletedialog').style.display!="none")
  722.         deleteQuery();
  723.     
  724.     document.getElementById("cover").style.display = "block";
  725.     helpCircleEdit();
  726. }
  727.  
  728. function helpCircleEdit() {
  729.     circleObject('edit',
  730.         function () {
  731.             showHelpText(0,
  732.                 function () {
  733.                     setTimeout( "helpCircleNewIcon()", 10000);
  734.                 }
  735.             )
  736.         }
  737.     );
  738. }
  739.  
  740. function helpCircleNewIcon() {
  741.     hideHelpText(0,
  742.         function () {
  743.             circleObject('newicon',
  744.                 function () {
  745.                     showHelpText(1,
  746.                         function () {
  747.                             setTimeout( "helpCircleDeleteIcon()", 5000)
  748.                         }
  749.                     )
  750.                 }
  751.             )
  752.         }
  753.     );
  754. }
  755.  
  756. function helpCircleDeleteIcon() {
  757.     hideHelpText(1,
  758.         function () {
  759.             circleObject('deleteicon',
  760.                 function () {
  761.                     showHelpText(2,
  762.                         function () {
  763.                             setTimeout( "helpCirclePrintIcon()", 5000)
  764.                         }
  765.                     )
  766.                 }
  767.             )
  768.         }
  769.     );
  770. }
  771.  
  772. function helpCirclePrintIcon() {
  773.     hideHelpText(2,
  774.         function () {
  775.             circleObject('printicon',
  776.                 function () {
  777.                     showHelpText(3,
  778.                         function () {
  779.                             setTimeout( "helpCircleSearchIcon()", 5000)
  780.                         }
  781.                     )
  782.                 }
  783.             )
  784.         }
  785.     );
  786. }
  787.  
  788. function helpCircleSearchIcon() {
  789.     hideHelpText(3,
  790.         function () {
  791.             circleObject('searchicon',
  792.                 function () {
  793.                     showHelpText(4,
  794.                         function () {
  795.                             setTimeout( "helpCircleNavIcon()", 5000)
  796.                         }
  797.                     )
  798.                 }
  799.             )
  800.         }
  801.     );
  802. }
  803.  
  804. function helpCircleNavIcon() {
  805.     hideHelpText(4,
  806.         function () {
  807.             circleObject('controls',
  808.                 function () {
  809.                     showHelpText(5,
  810.                         function () {
  811.                             setTimeout( "helpCircleGrowIcon()", 10000)
  812.                         }
  813.                     )
  814.                 }
  815.             )
  816.         }
  817.     );
  818. }
  819.  
  820. function helpCircleGrowIcon() {
  821.     hideHelpText(5,
  822.         function () {
  823.             circleObject('grow',
  824.                 function () {
  825.                     showHelpText(6,
  826.                         function () {
  827.                             setTimeout( "helpThanks()", 4000)
  828.                         }
  829.                     )
  830.                 }
  831.             )
  832.         }
  833.     );
  834. }
  835.  
  836. function helpThanks() {
  837.     hideHelpText(6,
  838.         function () {
  839.             document.getElementById("help_circle_canvas").style.display = "none";
  840.                     showHelpText(7,
  841.                         function () {
  842.                             setTimeout( "helpDone()", 4000)
  843.                         }
  844.                     )
  845.         }
  846.     );
  847. }
  848.  
  849.  
  850. function helpDone() {
  851.     hideHelpText(7, null);
  852.     document.getElementById("help_circle_canvas").style.display = "none";
  853.     document.getElementById("cover").style.display = "none";
  854.  
  855.     showPrefs();
  856. }
  857.  
  858.  
  859.  
  860. function cancelHelp() {
  861.     help_running = false;
  862.     document.getElementById("help_circle_canvas").style.display = "none";
  863.     document.getElementById("help_text_"+language).style.display = "none";
  864.     document.getElementById("cover").style.display = "none";
  865. }
  866.  
  867. function handleKeyPress(e) {
  868.     if (e.charCode == 27 && help_running) cancelHelp();
  869. }
  870.  
  871. function circleObject(obj, callBack) {
  872.     if (!help_running) return false;
  873.     
  874.     if (obj == new String(obj)) obj = document.getElementById(obj);
  875.     
  876.     help_canvas = document.getElementById("help_circle_canvas");
  877.     
  878.     help_canvas.style.display = "block";
  879.     var help_context = help_canvas.getContext("2d");
  880.     
  881.     help_canvas.style.top = (getOffsetTop(obj) - 3) + "px";
  882.     help_canvas.style.left = (getOffsetLeft(obj) - 3) + "px";
  883.     help_canvas.style.bottom = "0 px";
  884.     help_canvas.style.right = "0 px";
  885.     
  886.     help_context.lineWidth = 1;
  887.     help_context.lineJoin = "bevel";
  888.     help_context.lineCap = "round";
  889.     help_context.strokeStyle = "rgba(255, 0, 0, 0.8)";
  890.     
  891.     help_context.clearRect(0, 0, 1000, 1000);
  892.     help_context.save();
  893.     
  894.     if (parseInt(document.defaultView.getComputedStyle(obj, '').getPropertyValue("width")) > 20) {
  895.         help_canvas_x_stretch = parseInt(document.defaultView.getComputedStyle(obj, '').getPropertyValue("width"))/30;
  896.     } else {
  897.         help_canvas_x_stretch = 1;
  898.     }
  899.     
  900.     if (parseInt(document.defaultView.getComputedStyle(obj, '').getPropertyValue("height")) > 20) {
  901.         help_canvas_y_stretch = parseInt(document.defaultView.getComputedStyle(obj, '').getPropertyValue("height"))/30;
  902.     } else {
  903.         help_canvas_y_stretch = 1;
  904.     }
  905.     
  906.     help_context.scale(help_canvas_x_stretch, help_canvas_y_stretch);
  907.     
  908.     help_context.beginPath();
  909.     help_context.moveTo(5, 10);
  910.     
  911.     var i=0;
  912.     
  913.     var inter = setInterval(
  914.         function () {
  915.             //alert(i);
  916.             switch (i) {
  917.                 case 0:
  918.                     help_context.bezierCurveTo(5, 0, 20, 10, 20, 20);
  919.                     break;
  920.                 case 1:
  921.                     help_context.beginPath();
  922.                     help_context.moveTo(20, 10);
  923.                     help_context.bezierCurveTo(30, 20, 20, 30, 15, 20);
  924.                     break;
  925.                 case 2:
  926.                     help_context.beginPath();
  927.                     help_context.moveTo(20, 30);
  928.                     help_context.bezierCurveTo(10, 35, 5, 25, 5, 10);
  929.                     break;
  930.                 case 3:
  931.                     help_context.beginPath();
  932.                     help_context.moveTo(5, 25);
  933.                     help_context.bezierCurveTo(2, 7, 9, 10, 0, 0);
  934.                     break;
  935.                 //Default:
  936.                 case 4:
  937.                     clearInterval(inter);
  938.                     help_context.restore();
  939.                     if (callBack != null) callBack();
  940.                     break;
  941.             }
  942.             help_context.stroke();
  943.             i++;
  944.         }, 70);
  945. }
  946.  
  947. function showHelpText(which, callBack) {
  948.     if (!help_running) return false;
  949.     
  950.     var help_text = document.getElementById("help_text_"+language);
  951.     help_text.style.display = "block";
  952.     
  953.     var messages = help_text.getElementsByTagName("div");
  954.     if (which > 0) messages[which-1].style.opacity = 0;
  955.     
  956.     fade(messages[which], 0, 0.9, 
  957.         function() {
  958.             //setTimeout("help_text_shadow.paint()", 100);
  959.             callBack();
  960.         });
  961. }
  962.  
  963. function hideHelpText(which, callBack) {
  964.     var help_text = document.getElementById("help_text_"+language);
  965.     
  966.     
  967.     var messages = help_text.getElementsByTagName("div");
  968.     //if (which > 0) messages[which-1].style.opacity = 0;
  969.     
  970.     fade(messages[which], 0.9, 0, 
  971.         function () {
  972.             help_text.style.display = "none";
  973.             callBack();
  974.         });
  975. }
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982. /* Fade (for help text) */
  983. function fade(el, from, to, callBack) {
  984.     if (el == new String(el)) el = document.getElementById(el);
  985.     //so we can pass a string for the id or an element
  986.      
  987.     if (from == -1) // kalleboo mod
  988.          from = el.style.opacity;
  989.      
  990.     var dif = Math.abs(to - from);
  991.     
  992.     var pi = Math.PI;
  993.     var theta = 0;
  994.     
  995.     //if (to < from && el.style.opacity > 0) return;
  996.     
  997.     var inter = setInterval(
  998.         function() {
  999.             if (theta < 90) {
  1000.                 theta += 10;
  1001.                 if (to > from) 
  1002.                     var step = Math.sin((theta*pi)/180);
  1003.                 else 
  1004.                     var step = Math.cos((theta*pi)/180);
  1005.                 var opac = dif*step;
  1006.                 el.style.opacity = Math.max(0.01, opac); //fixing safari glitch
  1007.             } else {
  1008.                 el.style.opacity = to;
  1009.                 clearInterval(inter);
  1010.                 if (callBack != null) callBack();
  1011.             }
  1012.         }, 100);
  1013. }
  1014.  
  1015. /* Quicker fade (for hiding controls) */
  1016. function qfade(el) {
  1017.     el = document.getElementById(el);
  1018.      
  1019.     var from = el.style.opacity;
  1020.     var to = getTO();
  1021.  
  1022.     var pi = Math.PI;
  1023.     var theta = 0;
  1024.  
  1025.     var inter = setInterval(
  1026.         function() {
  1027.             to = getTO();
  1028.             
  1029.             if (theta < 90) {
  1030.                 theta += 20;
  1031.                 if (to > from) 
  1032.                     var step = Math.sin((theta*pi)/180);
  1033.                 else 
  1034.                     var step = Math.cos((theta*pi)/180);
  1035.                     
  1036.                 var opac = Math.abs(to - from) * step;
  1037.                 el.style.opacity = Math.max(0.01, opac); //fixing safari glitch
  1038.             } else {
  1039.                 el.style.opacity = getTO();
  1040.                 clearInterval(inter); // inter
  1041.             }
  1042.         }, 50);
  1043.         
  1044.     return inter;
  1045. }
  1046.  
  1047.  
  1048.  
  1049.  
  1050. function setFontSize() {
  1051.     document.getElementById("edit").style.fontSize = document.getElementById("fontsize").value;
  1052.     widget.setPreferenceForKey(document.getElementById("fontsize").value, "font-size");
  1053. }
  1054.  
  1055.  
  1056.  
  1057.  
  1058. /* FLIP */
  1059. function showPrefs() {
  1060.     flipped = true;
  1061.     var front = document.getElementById("front");
  1062.     var back = document.getElementById("back");
  1063.     
  1064.     if (window.widget)
  1065.         widget.prepareForTransition("ToBack");        // freezes the widget so that you can change it without the user noticing
  1066.     
  1067.     front.style.display="none";        // hide the front
  1068.     back.style.display="block";        // show the back
  1069.     
  1070.     if (window.widget)
  1071.         setTimeout ('widget.performTransition();', 0);        // and flip the widget over    
  1072. }
  1073.  
  1074. function hidePrefs() {
  1075.     fipped = false;
  1076.     var front = document.getElementById("front");
  1077.     var back = document.getElementById("back");
  1078.     
  1079.     if (window.widget)
  1080.         widget.prepareForTransition("ToFront");        // freezes the widget and prepares it for the flip back to the front
  1081.     
  1082.     back.style.display="none";            // hide the back
  1083.     front.style.display="block";        // show the front
  1084.     
  1085.     if (window.widget)
  1086.         setTimeout ('widget.performTransition();', 0);        // and flip the widget back to the front
  1087. }
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093.  
  1094. /* RESIZE */
  1095. function minwidth()     {    return 258;    }
  1096. function minheight()     {    return 261;    }
  1097.  
  1098. function maxwidth()     {    return 570;    }
  1099. function maxheight()     {    return 600;    }
  1100.  
  1101. var lastPos;        // tracks where the last mouse position was throughout the drag
  1102.  
  1103. function mouseDown(event) {
  1104.  
  1105.     var x = event.x + window.screenX;        // the placement of the click
  1106.     var y = event.y + window.screenY;
  1107.     
  1108.     document.addEventListener("mousemove", mouseMove, true);      // begin tracking the move
  1109.     document.addEventListener("mouseup", mouseUp, true);        // and notify when the drag ends
  1110.  
  1111.     lastPos = {x:x, y:y};        // track where the initial mouse down was, for later comparisons
  1112.                                 // the mouseMove function
  1113.  
  1114.     event.stopPropagation();
  1115.     event.preventDefault();
  1116. }
  1117.  
  1118. function mouseMove(event) {    
  1119.  
  1120.     var screenX = event.x + window.screenX;        // retrieves the current mouse position
  1121.     var screenY = event.y + window.screenY;
  1122.         
  1123.     var deltaX = 0;        // will hold the change since the last mouseMove event
  1124.     var deltaY = 0;
  1125.  
  1126.     if ( (window.outerWidth + (screenX - lastPos.x)) >= 60 ) {         // sets a minimum width constraint
  1127.         deltaX = screenX - lastPos.x;                                // if we're greater than the constraint,
  1128.         lastPos.x = screenX;                                        // save the change and update our past position
  1129.     }
  1130.  
  1131.     if ( (window.outerHeight + (screenY - lastPos.y)) >= 25 ) {        // setting contrains for the heght
  1132.         deltaY = screenY - lastPos.y;
  1133.         lastPos.y = screenY;
  1134.     }
  1135.     
  1136.     if (window.innerWidth+deltaX > maxwidth())
  1137.         deltaX = maxwidth()-window.innerWidth;
  1138.     
  1139.     if (window.innerWidth+deltaX < minwidth())
  1140.         deltaX = minwidth()-window.innerWidth;
  1141.         
  1142.     if (window.innerHeight+deltaY > maxheight())
  1143.         deltaY = maxheight()-window.innerHeight;
  1144.     
  1145.     if (window.innerHeight+deltaY < minheight())
  1146.         deltaY = minheight()-window.innerHeight;
  1147.     
  1148.     window.resizeBy(deltaX, deltaY);    // resizes the widget to follow the mouse movement
  1149.  
  1150.     event.stopPropagation();
  1151.     event.preventDefault();
  1152. }
  1153.  
  1154. function mouseUp(event) {
  1155.     document.removeEventListener("mousemove", mouseMove, true);
  1156.     document.removeEventListener("mouseup", mouseUp, true);    
  1157.  
  1158.     widget.setPreferenceForKey(window.outerWidth, "pad-width");
  1159.     widget.setPreferenceForKey(window.outerHeight, "pad-height");
  1160.  
  1161.     event.stopPropagation();
  1162.     event.preventDefault();
  1163. }
  1164.  
  1165. /* Reg */
  1166.  
  1167. function register() {
  1168.     if (NotePad) NotePad.registerWidget();
  1169. }
  1170.  
  1171. function buy() {
  1172.     if (NotePad) NotePad.buyWidget();
  1173. }
  1174.  
  1175. function showNag(days_left) {
  1176.     //alert('days left (js): ' + days_left);
  1177.     isNagging = true;
  1178.     
  1179.     // show the register stickers on the back
  1180.         document.getElementById("backreground").style.display = "";
  1181.         document.getElementById("backbuynow").style.display = "";
  1182.     //
  1183.     
  1184.     document.getElementById("front").style.display = "none";
  1185.     document.getElementById("back").style.display = "none";
  1186.     window.resizeTo(341, 134);
  1187.     
  1188.     //I'll forgo doing this in a nice DOM way for the moment:
  1189.     var cont = document.createElement("div");
  1190.     cont.id = "buy_panel";
  1191.     
  1192.     var buy_p_text =  document.createElement("div");
  1193.     buy_p_text.id = "buy_panel_text";
  1194.     cont.appendChild(buy_p_text);
  1195.     
  1196.     buy_p_text.innerHTML += getLocalString("daysleft_pre") + '<span id="widget_name">NotePad</span>' + getLocalString("daysleft_mid") + days_left + getLocalString("daysleft_post");
  1197.     if (10 - days_left != 1) buy_p_text.innerHTML += getLocalString("daysleft_plural");
  1198.     buy_p_text.innerHTML += '.<br />'+getLocalString("daysleft_thanks")+'\n';
  1199.     var buy_p_buts =  document.createElement("div");
  1200.     buy_p_buts.id = "buy_buttons";
  1201.     cont.appendChild(buy_p_buts);
  1202.     
  1203.     buy_p_buts.innerHTML += '<a href="#" onclick="NotePad.buyWidget()">'+getLocalString("buy")+'</a>\n';
  1204.     buy_p_buts.innerHTML += '<a href="#" onclick="NotePad.registerWidget()">'+getLocalString("register")+'</a>\n';
  1205.     
  1206.     var counter = document.createElement("span");
  1207.     counter.id = "counter";
  1208.     counter.innerText = 7;
  1209.     
  1210.     buy_p_buts.appendChild(counter);
  1211.     
  1212.     document.body.appendChild(cont);
  1213.     
  1214.     var count_down = 7;
  1215.     
  1216.     var inter = setInterval(
  1217.         function () {
  1218.             if (--count_down >= 0) {
  1219.                 counter.innerText = count_down;
  1220.             } else {
  1221.                 clearInterval(inter);
  1222.                 window.resizeTo(250, 230);
  1223.                 if (!flipped) {
  1224.                     document.getElementById("front").style.display = "block";
  1225.                 } else {
  1226.                     document.getElementById("back").style.display = "block";
  1227.                     //document.getElementById("register_buy").style.display = "block";
  1228.                 }
  1229.                 
  1230.                 document.body.removeChild(cont);
  1231.             }
  1232.         }, 1000);
  1233. }